import 'package:day_demo/src/square/square_consumer_hotline/square_consumer_hotline_sheet.dart';
import 'package:day_demo/src/square/widgets/clearable_texfield.dart';
import 'package:day_demo/src/square/widgets/ink_text_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class OrgSquareEditingHeadingWidget extends StatefulWidget {
  const OrgSquareEditingHeadingWidget({
    super.key,
    this.heading = '',
  });
  final String heading;
  @override
  State<OrgSquareEditingHeadingWidget> createState() =>
      _OrgSquareEditingHeadingWidgetState();
}

class _OrgSquareEditingHeadingWidgetState
    extends State<OrgSquareEditingHeadingWidget> {
  TextEditingController controller = TextEditingController();
  InkTextButtonController buttonController =
      InkTextButtonController(InkTextButtonValue());
  RxBool isSameHeading = false.obs;
  String get heading => widget.heading;

  @override
  void initState() {
    controller.text = heading;
    controller.addListener(addTextChangedListener);
    super.initState();
  }

  void addTextChangedListener() async {
    var enabled = controller.text.isNotEmpty &&
        controller.text.characters.length >= 4 &&
        controller.text.characters.length <= 30;
    buttonController.value = buttonController.value.copyWith(
      enabled: enabled,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        SizedBox(
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          child: GestureDetector(
            onTap: () {
              SystemUtils.hideSoftKeyboard(context);
            },
            child: Container(
              color: Colors.transparent,
            ),
          ),
        ),
        Column(
          children: [
            Container(
              color: Color(0xffF5F8FE),
              height: 12,
              width: double.infinity,
            ),
            Container(
              color: Colors.white,
              child: IQClearTexField(
                autofocus: true,
                maxLines: null,
                hintText: '广场名称',
                controller: controller,
                inputFormatters: [
                  LengthLimitingTextInputFormatter(30),
                  FilteringTextInputFormatter.deny(RegExp(r'\s')),
                ],
                onChanged: (_) {
                  if (isSameHeading.value) {
                    isSameHeading.value = false;
                  }
                },
              ),
            ),
            Obx(() => Container(
                  color: Colors.white,
                  constraints: const BoxConstraints(minHeight: 68),
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        padding: const EdgeInsets.only(
                            left: 16, right: 16, top: 8, bottom: 16),
                        color: Color(0xffF5F8FE),
                        width: double.infinity,
                        alignment: Alignment.topLeft,
                        child: Text(
                          statusText(),
                          style: TextStyle(
                            fontSize: 14,
                            color: isSameHeading.value
                                ? Color(0xFFD54941)
                                : Color(0xFF828DA5),
                          ),
                        ),
                      ),
                      if (isSameHeading.value)
                        const SizedBox(
                          height: 20,
                        ),
                    ],
                  ),
                )),
            Container(
              padding: const EdgeInsets.fromLTRB(16, 24, 16, 12),
              color: Colors.white,
              child: InkTextButton(
                title: '提交审核',
                controller: buttonController,
                onTap: () {
                  SquareConsumerHotlineSheet.show(context);
                  // SystemUtils.hideSoftKeyboard(context);
                  // if (controller.text == heading) {
                  //   isSameHeading.value = true;
                  // }
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  String statusText() {
    return isSameHeading.value
        ? "该名称与原来一直，请重新输入"
        : '长度限制4-30个字符;\n一年内可修改2次。今年已修改1次。';
  }

  @override
  void deactivate() {
    SystemUtils.hideSoftKeyboard(context);
    super.deactivate();
  }

  @override
  void dispose() {
    controller.removeListener(addTextChangedListener);
    controller.dispose();
    super.dispose();
  }
}

class SystemUtils {
  static hideSoftKeyboard(BuildContext? context) {
    if (context == null) {
      final primaryFocus = FocusManager.instance.primaryFocus;
      if (primaryFocus != null && primaryFocus.hasPrimaryFocus) {
        primaryFocus.unfocus();
      }
      return;
    }
    closeKeyboard(context);
  }
}

void closeKeyboard(BuildContext context) {
  FocusScopeNode currentFocus = FocusScope.of(context);
  if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
    FocusManager.instance.primaryFocus?.unfocus();
  }
}
